ES6 其它

函数参数的解构赋值

ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构,如下

1
2
3
4
5
actions: {
increment ({ commit }) {
commit('increment')
}
}

上面increment 这个 action 会被传入一个 Object,这个 Object 里面可能有很多的方法和属性,而只需要 commit 方法,所以可以写成这样,也可以转化为

1
2
3
4
5
actions: {
increment (obj) {
obj.commit('increment')
}
}

数组的扩展运算符

扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。

1
2
3
4
5
6
console.log(...[1,2,3]) 
// 1 2 3

// 拆分字符串
console.log([...'hello']);
// [ 'h', 'e', 'l', 'l', 'o' ]
本文结束,感谢您的阅读